<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="75%" destroy-on-close @closed="$emit('closed')" align-center :close-on-click-modal="false" :close-on-press-escape="mode != 'show' ? false : true">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="right">
            <el-row :gutter="1">
                <el-col :span="6">
                    <el-form-item label="工序名称" prop="working_name">
                        <el-input v-model="form.working_name" placeholder="请输入工序名称" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="工序编码" prop="working_code">
                        <el-input v-model="form.working_code" placeholder="请输入工序编码" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="报工权限" prop="sort">
                        <el-select v-model="form.report_auth" placeholder="请选择" clearable>
                            <el-option label="全部人员" :value="1"></el-option>
                            <el-option label="部门" :value="2"></el-option>
                            <el-option label="部门人员" :value="3"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="报工人" prop="sort">
                        <el-select v-model="form.purchase_staff" filterable clearable placeholder="请选择报工人" style="width:100%">
                            <el-option v-for="item in adminList" :key="item.id" :label="item.name" :value="item.id"/>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="6">
                    <el-form-item label="工资类型" prop="working_wages">
                        <el-radio-group v-model="form.working_wages" class="ml-4">
                            <el-radio :label="1" size="large">计件</el-radio>
                            <el-radio :label="2" size="large">计时</el-radio>
                        </el-radio-group>
                        <!-- <el-select v-model="form.report_auth" placeholder="请选择" clearable>
                            <el-option label="计件" :value="1"></el-option>
                            <el-option label="计时" :value="2"></el-option>
                        </el-select> -->
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="计件工资" prop="working_price" v-if="form.working_wages == 1">
                        <el-input v-model="form.working_price" placeholder="请输入计件工资" clearable></el-input>
                    </el-form-item>
                    <el-form-item label="计时工资" prop="working_price" v-if="form.working_wages == 2">
                        <el-input v-model="form.working_price" placeholder="请输入计时工资" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="不良品项" prop="rejects_all">
                        <el-select v-model="form.rejects_all" placeholder="请选择" clearable filterable multiple collapse-tags collapse-tags-tooltip max-collapse-tags="2">
                            <el-option v-for="item in reasonList" :key="item.id" :label="item.rejects_name" :value="item.id"/>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="报工配比" prop="ratio">
                        <el-input v-model="form.ratio" placeholder="请输入报工配比" clearable></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="工序要求" prop="content">
                        <el-input v-model="form.content" placeholder="请输入工序要求" clearable type="textarea"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="工序SOP文档" prop="sop">
                        <sc-upload-file v-model="form.sop" :limit="1">
                            <el-button type="primary" icon="el-icon-upload">选择文件</el-button>
                        </sc-upload-file>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-col :span="6">
                <el-form-item label="工序图纸" prop="drawing">
                    <sc-upload height="100" width="100" v-model="form.drawing"></sc-upload>
                </el-form-item>
            </el-col>
            <!-- <el-form-item label="工序备注" prop="remark">
                <el-input v-model="form.remark" placeholder="请输入工序备注" clearable type="textarea"></el-input>
            </el-form-item> -->
            <el-form-item label="工序采集项" prop="detail">
                <sc-form-table ref="table" v-model="form.detail" :addTemplate="addTemplate" placeholder="暂无数据" height="300">
                    <el-table-column label="采集类型" align="center" prop="collection_type">
                        <template #default="scope">
                            <el-select v-model="scope.row.collection_type" placeholder="请选择" filterable>
                                <el-option label="文本" :value="1"></el-option>
                                <el-option label="时间" :value="2"></el-option>
                                <el-option label="数字" :value="3"></el-option>
                                <el-option label="照片" :value="4"></el-option>
                            </el-select>
                        </template>
                    </el-table-column>
                    <el-table-column label="采集名称" align="center" prop="collection_name">
                        <template #default="scope">
                            <el-input v-model="scope.row.collection_name" placeholder="请输入采集名称" clearable></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column label="是否必填" align="center" prop="is_must">
                        <template #default="scope">
                            <el-switch v-model="scope.row.is_must" :active-value="1" :inactive-value="0"></el-switch>
                        </template>
                    </el-table-column>
                </sc-form-table>
            </el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增工序',
				edit: '编辑工序',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
                id:"",
                working_name:"",
                working_code:"",
                drawing:"",
                rejects_all:"",
                ratio:"",
                content:"",
                sop:"",
                working_wages:1,
                working_price:"",
                power:"",
                sort: "",
                detail: [],
			},
			//验证规则
			rules: {
                working_name: [
					{ required: true, message: '请输入工序名称' }
				],
			},
            addTemplate: {},
            unitList:[],
            adminList: [],
            reasonList: [],
		}
	},
	mounted() {
        // this.getUnit()
        this.getUser()
        this.getReason()
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.goods.process.edit.post(this.form);
					} else {
						res = await this.$API.goods.process.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		async setData(data) {
            this.form.id = data.id
            var res = await this.$API.goods.process.detail.get({id:data.id});
            this.form.working_name = res.data.working_name
            this.form.sort = res.data.sort
            this.form.remark = res.data.remark
            let array = []
            res.data.detail.map(item=>{
                array.push({
                    id:item.id,
                    custom_name:item.custom_name,
                    standard:item.standard,
                    unit_id:item.unit_id,
                })
            })
            this.form.detail = array
		},
        // 获取单位
        async getUnit(){
            var res = await this.$API.goods.processUnit.all.get();
            this.unitList = res.data
        },
        // 获取报工人
        async getUser(){
            var res = await this.$API.system.admin.all.get();
            this.adminList = res.data;
        },
        // 获取不良品
        async getReason(){
            var res = await this.$API.goods.reason.all.get();
            this.reasonList = res.data;
        },
	}
}
</script>

<style>
    .el-input-group__append{
        padding: 0 8px;
    }
</style>
